﻿Imports System.Data.SqlClient
Namespace BaseClass
    '---------数据库
    Public Class DBOperation
#Region "打开数据库连接"
        Public Function OpenDB(ByRef Tconn As SqlConnection, Optional ByRef ErrString As String = "") As Boolean
            Try
                If Tconn Is Nothing Then
                    Tconn = New SqlConnection(Connstr)
                End If
                If Tconn.State <> ConnectionState.Open Then
                    Tconn = New SqlConnection(Connstr)
                    Tconn.Open()
                End If
                Return True
            Catch ex As Exception
                ErrString = String.Format("{0}{1}ERR Code:3", ex.Message, vbCrLf)
                Return False
            End Try
            Return False
        End Function
#End Region

#Region "执行SQL查询"
        Public Function QuerySQL(ByVal sqlstr As String, ByRef ds As DataSet, Optional ByRef ErrString As String = "") As Boolean
            If Trim(sqlstr) = String.Empty Then
                ErrString = String.Format("错误的参数!{0}ERR Code:1", vbCrLf)
                Return False
            End If
            ds = New DataSet
            Dim Tconn As New SqlConnection(Connstr)
            If Dcontrol.OpenDB(Tconn, ErrString) = False Then
                Return False
            Else
                Try
                    Dim da As New SqlDataAdapter(sqlstr, Tconn)
                    da.Fill(ds)
                    Tconn.Dispose()
                    Return True
                Catch ex As Exception
                    Tconn.Dispose()
                    ErrString = String.Format("{0}{1}ERR Code:4", ex.Message, vbCrLf)
                    Return False
                End Try
            End If
        End Function
#End Region
#Region "执行SQL命令"
        Public Function ExecSQL(ByVal sqlstr As String, Optional ByRef ErrString As String = "") As Boolean
            If Trim(sqlstr) = String.Empty Then
                ErrString = String.Format("错误的参数!{0}ERR Code:1", vbCrLf)
                Return False
            End If
            Dim tconn As New SqlConnection(Connstr)
            If Dcontrol.OpenDB(tconn, ErrString) = False Then
                tconn = Nothing
                Return False
            End If
            Dim cmd As New SqlCommand(sqlstr, tconn)
            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                cmd = Nothing
                tconn.Dispose()
                Return True
            Catch ex As Exception
                tconn.Dispose()
                ErrString = String.Format("{0}{1}ERR Code:4", ex.Message, vbCrLf)
                Return False
            End Try
        End Function
#End Region






















    End Class
End Namespace


